import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Calculator //主类Calculator定义开始
{
    public static void main(String[] args) {
        CalculatorFrame frame = new CalculatorFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
} //主类Calculator定义结束

class CalculatorFrame extends JFrame //CalculatFrame类定义开始
{
    public CalculatorFrame() {
        setTitle("计算器");
        CalculatorPanel panel = new CalculatorPanel(); //实例化CalculatorPanel类成panel对象
        add(panel); //将panel对象添加CalculatorFrame中
        pack();
    }
}

class CalculatorPanel extends Panel
{
    private JButton display;
    private JPanel panel;
    private double result; 
    private String lastCommand;
    private boolean start;
    private boolean flag;
    public CalculatorPanel() {
        setLayout(new BorderLayout());
        result = 0;
        lastCommand = "=";
        start = true;
        flag = true;
        display = new JButton("0"); //实例化display按钮
        display.setEnabled(false); //将按钮设置为不可操作，只能显示字符串
        add(display, BorderLayout.NORTH); //将按钮添加到窗口最顶端
        ActionListener insert = new InsertAction(); //实例化侦听器器类InsertAction
        ActionListener command = new CommandAction(); //实例化侦听器器类CommandAction
        panel = new JPanel(); //实例化JPanel类
        panel.setLayout(new GridLayout(4, 5)); //为panel设置布局管理器:布局为4行5列从左向右添加20个按钮
        addButton("7", insert);
        addButton("8", insert);
        addButton("9", insert);
        addButton("/", command);
        addButton("清零", command);
        addButton("4", insert);
        addButton("5", insert);
        addButton("6", insert);
        addButton("*", command);
        addButton("退格", command);
        addButton("1", insert);
        addButton("2", insert);
        addButton("3", insert);
        addButton("-", command);
        addButton("sqrt", command);
        addButton("0", insert);
        addButton(".", insert);
        addButton("=", command);
        addButton("+", command);
        addButton("1/x", command);
        add(panel, BorderLayout.CENTER);
    } //CalculatorPanel类定义结束

    private void addButton(String label, ActionListener listener) //方法定义开始
    {
        JButton button = new JButton(label);
        button.addActionListener(listener);
        panel.add(button);
    } //addButton方法定义结束

    public void calculate(double x, String command) // calculate方法定义开始
    {
        if (lastCommand.equals("+")) result += x;
        else if (lastCommand.equals("-")) result -= x;
        else if (lastCommand.equals("/")) {
            if (x != 0)
                result /= x;
            else {
                display.setText("除数不能为0");
                start = false;
                flag = false;
                return;
            }
        } else if (lastCommand.equals("*")) result *= x;
        else if (command.equals("1/x")) {
            if (x != 0)
                result = 1 / x;
            else {
                display.setText("除数不能为0");
                start = false;
                flag = false;
                return;
            }
        } else if (command.equals("sqrt")) {
            if (x >= 0)
                result = Math.sqrt(x);
            else {
                display.setText("函数输入无效");
                start = false;
                flag = false;
                return;
            }
        } else if (lastCommand.equals("=")) result = x;
        display.setText("" + result);
    } //calculate方法定义结束

    private class InsertAction implements ActionListener // InsertAction事件侦听器类定义开始
    {
        public void actionPerformed(ActionEvent event) {
            String input = event.getActionCommand();
            if (start && flag) {
                display.setText("");
                start = false;
            }
            if (flag)
                display.setText(display.getText() + input);
        }
    } //InsertAction事件侦听器类定义结束

    private class CommandAction implements ActionListener // CommandAction事件侦听器类定义开始
    {
        public void actionPerformed(ActionEvent event) {
            String command = event.getActionCommand();
            if (command.equals("清零")) {
                display.setText("0");
                start = true;
                flag = true;
                command = "=";
            } else if (start && flag) {
                if (command.equals("-")) {
                    display.setText(command);
                    start = false;
                } else if ((command.equals("1/x") || command.equals("sqrt")) && flag)
                    calculate(Double.parseDouble(display.getText()), command);
                else if (flag)
                    lastCommand = command;
            } else {
                if (command.equals("退格") && flag) {
                    String s = display.getText();
                    char[] s1 = s.toCharArray();
                    if (s.length() >= 2) {
                        String s2 = new String(s1, 0, s.length() - 1);
                        display.setText(s2);
                    } else {
                        display.setText("0");
                        start = true;
                    }
                } else if (flag) {
                    calculate(Double.parseDouble(display.getText()), command);

                    lastCommand = command;
                    start = true;
                }
            }
        }
    } //CommandAction事件侦听器类定义结束
}